home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 22 / PCPP #22.iso / Quake2 / q2source_12_11 / utils3 / texpaint / win_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-29  |  10.3 KB  |  475 lines

  1. #include "texpaint.h"
  2.  
  3. HINSTANCE    main_instance;
  4.  
  5. int        screen_width, screen_height;
  6.  
  7. HWND    mainwindow;
  8. HWND    camerawindow;
  9. HWND    palettewindow;
  10. HWND    skinwindow;
  11.  
  12. /*
  13. =================
  14. Sys_Error
  15.  
  16. For abnormal program terminations
  17. =================
  18. */
  19. void Sys_Error (char *error, ...)
  20. {
  21.     va_list argptr;
  22.     char    text[1024];
  23.     char    text2[1024];
  24.     int        err;
  25.  
  26.     err = GetLastError ();
  27.  
  28.     va_start (argptr,error);
  29.     vsprintf (text, error,argptr);
  30.     va_end (argptr);
  31.  
  32.     sprintf (text2, "%s\nGetLastError() = %i", text, err);
  33.     MessageBox(mainwindow, text2, "Error", 0 /* MB_OK */ );
  34.  
  35.     exit (1);
  36. }
  37.  
  38.  
  39. /*
  40. ======================================================================
  41.  
  42. FILE DIALOGS
  43.  
  44. ======================================================================
  45. */
  46.  
  47. qboolean    modified;
  48. qboolean    modified_past_autosave;
  49.  
  50. qboolean ConfirmModified (void)
  51. {
  52.     if (!modified)
  53.         return true;
  54.  
  55.     if (MessageBox (mainwindow, "This will lose changes to the skin"
  56.         , "warning", MB_OKCANCEL) == IDCANCEL)
  57.         return false;
  58.     return true;
  59. }
  60.  
  61. OPENFILENAME ofn;       /* common dialog box structure   */ 
  62. char szDirName[MAX_PATH];    /* directory string              */ 
  63. char szFile[260];       /* filename string               */ 
  64. char szFileTitle[260];  /* file title string             */ 
  65. char szSkinFilter[260] =     /* filter string                 */ 
  66.     "Skin texture (*.lbm *.pcx)\0*.lbm;*.pcx\0\0";
  67. char szFrameFilter[260] =     /* filter string                 */ 
  68.     "Model frame (*.tri)\0*.tri\0\0";
  69. char chReplace;         /* string separator for szFilter */ 
  70. int i, cbString;        /* integer count variables       */ 
  71. HANDLE hf;              /* file handle                   */ 
  72.  
  73. void OpenSkinDialog (void)
  74. {
  75. //    strcpy (szDirName, ValueForKey (project_entity, "basepath") );
  76. //    strcat (szDirName, "\\maps");
  77.  
  78.     /* Place the terminating null character in the szFile. */ 
  79.  
  80.     szFile[0] = '\0'; 
  81.  
  82.     /* Set the members of the OPENFILENAME structure. */ 
  83.  
  84.     ofn.lStructSize = sizeof(OPENFILENAME); 
  85.     ofn.hwndOwner = mainwindow; 
  86.     ofn.lpstrFilter = szSkinFilter; 
  87.     ofn.nFilterIndex = 1; 
  88.     ofn.lpstrFile = szFile; 
  89.     ofn.nMaxFile = sizeof(szFile); 
  90.     ofn.lpstrFileTitle = szFileTitle; 
  91.     ofn.nMaxFileTitle = sizeof(szFileTitle); 
  92.     ofn.lpstrInitialDir = szDirName; 
  93.     ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | 
  94.         OFN_FILEMUSTEXIST; 
  95.  
  96.     /* Display the Open dialog box. */ 
  97.  
  98.     if (!GetOpenFileName(&ofn))
  99.         return;    // canceled
  100.  
  101.     Skin_LoadFile (ofn.lpstrFile);    
  102. }
  103.  
  104. void OpenFrameDialog (void)
  105. {
  106. //    strcpy (szDirName, ValueForKey (project_entity, "basepath") );
  107. //    strcat (szDirName, "\\maps");
  108.  
  109.     /* Place the terminating null character in the szFile. */ 
  110.  
  111.     szFile[0] = '\0'; 
  112.  
  113.     /* Set the members of the OPENFILENAME structure. */ 
  114.  
  115.     ofn.lStructSize = sizeof(OPENFILENAME); 
  116.     ofn.hwndOwner = mainwindow; 
  117.     ofn.lpstrFilter = szFrameFilter; 
  118.     ofn.nFilterIndex = 1; 
  119.     ofn.lpstrFile = szFile; 
  120.     ofn.nMaxFile = sizeof(szFile); 
  121.     ofn.lpstrFileTitle = szFileTitle; 
  122.     ofn.nMaxFileTitle = sizeof(szFileTitle); 
  123.     ofn.lpstrInitialDir = szDirName; 
  124.     ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | 
  125.         OFN_FILEMUSTEXIST; 
  126.  
  127.     /* Display the Open dialog box. */ 
  128.  
  129.     if (!GetOpenFileName(&ofn))
  130.         return;    // canceled
  131.  
  132.     LoadTriFile (ofn.lpstrFile);
  133. }
  134.  
  135. void SaveSkinDialog (void)
  136. {
  137. //    strcpy (szDirName, ValueForKey (project_entity, "basepath") );
  138. //    strcat (szDirName, "\\maps");
  139.  
  140.     /* Place the terminating null character in the szFile. */ 
  141.  
  142.     szFile[0] = '\0'; 
  143.  
  144.     /* Set the members of the OPENFILENAME structure. */ 
  145.  
  146.     ofn.lStructSize = sizeof(OPENFILENAME); 
  147.     ofn.hwndOwner = mainwindow; 
  148.     ofn.lpstrFilter = szSkinFilter; 
  149.     ofn.nFilterIndex = 1; 
  150.     ofn.lpstrFile = szFile; 
  151.     ofn.nMaxFile = sizeof(szFile); 
  152.     ofn.lpstrFileTitle = szFileTitle; 
  153.     ofn.nMaxFileTitle = sizeof(szFileTitle); 
  154.     ofn.lpstrInitialDir = szDirName; 
  155.     ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | 
  156.         OFN_FILEMUSTEXIST; 
  157.  
  158.     /* Display the Open dialog box. */ 
  159.  
  160.     if (!GetSaveFileName(&ofn))
  161.         return;    // canceled
  162.  
  163.     DefaultExtension (ofn.lpstrFile, ".lbm");
  164.     Skin_SaveFile (ofn.lpstrFile);    
  165.     strcpy (skin_filename, ofn.lpstrFile);
  166. }
  167.  
  168. //==========================================================================
  169.  
  170. BOOL bSetupPixelFormat(HDC hDC)
  171. {
  172.     static PIXELFORMATDESCRIPTOR pfd = {
  173.     sizeof(PIXELFORMATDESCRIPTOR),    // size of this pfd
  174.     1,                // version number
  175.     PFD_DRAW_TO_WINDOW |        // support window
  176.       PFD_SUPPORT_OPENGL |        // support OpenGL
  177.       PFD_DOUBLEBUFFER,        // double buffered
  178.     PFD_TYPE_RGBA,            // RGBA type
  179.     24,                // 24-bit color depth
  180.     0, 0, 0, 0, 0, 0,        // color bits ignored
  181.     0,                // no alpha buffer
  182.     0,                // shift bit ignored
  183.     0,                // no accumulation buffer
  184.     0, 0, 0, 0,             // accum bits ignored
  185.     32,                // 32-bit z-buffer    
  186.     0,                // no stencil buffer
  187.     0,                // no auxiliary buffer
  188.     PFD_MAIN_PLANE,            // main layer
  189.     0,                // reserved
  190.     0, 0, 0                // layer masks ignored
  191.     };
  192.     int pixelformat = 0;
  193.     PIXELFORMATDESCRIPTOR newp;
  194.  
  195.     if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
  196.     {
  197.         printf("%d",GetLastError());
  198.         Error ("ChoosePixelFormat failed");
  199.     }
  200.     if (!SetPixelFormat(hDC, pixelformat, &pfd))
  201.         Error ("SetPixelFormat failed");
  202.  
  203.     return TRUE;
  204. }
  205.  
  206.  
  207. /*
  208. ==============================================================================
  209.  
  210.   MENU
  211.  
  212. ==============================================================================
  213. */
  214.  
  215.  
  216. /* handle all WM_COMMAND messages here */
  217. LONG WINAPI CommandHandler (
  218.     HWND    hWnd,
  219.     WPARAM  wParam,
  220.     LPARAM  lParam)
  221. {
  222.     unsigned short    cmd;
  223.  
  224.     cmd = LOWORD(wParam);
  225.  
  226.     switch (cmd)
  227.     {
  228.         //
  229.         // file menu
  230.         //
  231.     case ID_FILE_RESAMPLESKIN:
  232.         ResampleSkin ();
  233.         break;
  234.  
  235.     case ID_FILE_NEWSKIN:
  236.         NewSkin ();
  237.         break;
  238.  
  239.     case ID_FILE_OPENFRAME:
  240.         OpenFrameDialog ();
  241.         break;
  242.  
  243.     case ID_FILE_OPENSKIN:
  244.         if (!ConfirmModified())
  245.             break;
  246.         OpenSkinDialog ();
  247.         break;
  248.  
  249.     case ID_FILE_RELOADSKIN:
  250.         if (!ConfirmModified())
  251.             break;
  252.         Skin_LoadFile (skin_filename);
  253.         break;
  254.  
  255.     case ID_FILE_SAVESKIN:
  256.         Skin_SaveFile (skin_filename);
  257.         break;
  258.  
  259.     case ID_FILE_SAVESKINAS:
  260.         SaveSkinDialog ();
  261.         break;
  262.     case ID_FILE_EXIT:
  263.         if (!ConfirmModified())
  264.             break;
  265.         PostQuitMessage (0);
  266.         break;
  267.  
  268.         //
  269.         // edit menu
  270.         //
  271.     case ID_EDIT_UNDO:
  272.         Undo();
  273.         break;
  274.     case ID_EDIT_REDO:
  275.         Redo();
  276.         break;
  277.  
  278.         //
  279.         // view menu
  280.         //
  281.     case ID_VIEW_MODELLINES:
  282.         model_lines ^= 1;
  283.         CheckMenuItem ( GetSubMenu (GetMenu(mainwindow), MENU_VIEW)
  284.             , ID_VIEW_MODELLINES
  285.             , MF_BYCOMMAND | (model_lines ? MF_CHECKED : MF_UNCHECKED)  );
  286.         InvalidateRect (camerawindow, NULL, false);
  287.         break;
  288.     case ID_VIEW_TEXTURELINES:
  289.         skin_lines ^= 1;
  290.         CheckMenuItem ( GetSubMenu (GetMenu(mainwindow), MENU_VIEW)
  291.             , ID_VIEW_TEXTURELINES
  292.             , MF_BYCOMMAND | (skin_lines ? MF_CHECKED : MF_UNCHECKED)  );
  293.         InvalidateRect (skinwindow, NULL, false);
  294.         break;
  295.     default:
  296.         return FALSE;
  297.     }
  298.  
  299.     return TRUE;
  300. }
  301.  
  302. /*
  303. ============
  304. WMAIN_WndProc
  305. ============
  306. */
  307. LONG WINAPI WMAIN_WndProc (
  308.     HWND    hWnd,
  309.     UINT    uMsg,
  310.     WPARAM  wParam,
  311.     LPARAM  lParam)
  312. {
  313.     LONG    lRet = 1;
  314.     RECT    rect;
  315.     HDC        maindc;
  316.  
  317.     GetClientRect(hWnd, &rect);
  318.  
  319.     switch (uMsg)
  320.     {
  321.     case WM_CREATE:
  322.         maindc = GetDC(hWnd);
  323.         bSetupPixelFormat(maindc);
  324.         break;
  325.     case WM_COMMAND:
  326.         lRet = CommandHandler (hWnd, wParam, lParam);
  327.         break;
  328.  
  329.     case WM_CLOSE:
  330.         if (!ConfirmModified())
  331.             break;
  332.         PostQuitMessage (0);
  333.         break;
  334.     default:
  335.         /* pass all unhandled messages to DefWindowProc */
  336.         lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
  337.         break;
  338.     }
  339.  
  340.     /* return 1 if handled message, 0 if not */
  341.     return lRet;
  342. }
  343.  
  344.  
  345.  
  346.  
  347. /*
  348. ==============
  349. Main_Create
  350. ==============
  351. */
  352. void Main_Create (HINSTANCE hInstance)
  353. {
  354.     WNDCLASS   wc;
  355.  
  356.     /* Register the class */
  357.     memset (&wc, 0, sizeof(wc));
  358.  
  359.     wc.style         = 0;
  360.     wc.lpfnWndProc   = (WNDPROC)WMAIN_WndProc;
  361.     wc.cbClsExtra    = 0;
  362.     wc.cbWndExtra    = 0;
  363.     wc.hInstance     = hInstance;
  364.     wc.hIcon         = 0;
  365.     wc.hCursor       = LoadCursor (NULL,IDC_ARROW);
  366.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  367.     wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU2);
  368.     wc.lpszClassName = "TEXPAINT_MAIN";
  369.  
  370.     if (!RegisterClass (&wc) )
  371.         Error ("WCam_Register: failed");
  372.  
  373.  
  374.     mainwindow = CreateWindow ("TEXPAINT_MAIN" ,
  375.         "Texpaint",
  376.         WS_OVERLAPPEDWINDOW |
  377.         WS_CLIPSIBLINGS |
  378.         WS_CLIPCHILDREN,
  379.         0,0,screen_width,screen_height,    // size
  380.         0,
  381.         NULL,        // no menu
  382.         hInstance,
  383.         NULL);
  384.     if (!mainwindow)
  385.         Error ("Couldn't create main window");
  386.         
  387. //    GetWindowInfo("mainwindow", &SavedInfo, NULL);    
  388.     ShowWindow (mainwindow, SW_SHOWDEFAULT);
  389. }
  390.  
  391.  
  392.  
  393.  
  394. BOOL SaveWindowInfo(const char *pszName, void *pvBuf, long lSize)
  395. {
  396.     LONG lres;
  397.     DWORD dwDisp;
  398.     HKEY  hKeyId;
  399.  
  400.     lres = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\id\\Texpaint", 0, NULL, 
  401.             REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKeyId, &dwDisp);
  402.     
  403.     if (lres != ERROR_SUCCESS)
  404.         return FALSE;
  405.  
  406.     lres = RegSetValueEx(hKeyId, pszName, 0, REG_BINARY, pvBuf, lSize);
  407.     
  408.     RegCloseKey(hKeyId);
  409.  
  410.     if (lres != ERROR_SUCCESS)
  411.         return FALSE;
  412.  
  413.     return TRUE;
  414. }
  415.  
  416.  
  417. BOOL GetWindowInfo(const char *pszName, void *pvBuf, long *plSize)
  418. {
  419.     HKEY  hKey;
  420.     long lres, lType, lSize;
  421.  
  422.     if (plSize == NULL)
  423.         plSize = &lSize;
  424.  
  425.     lres = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\id\\Texpaint", 0, KEY_READ, &hKey);
  426.     
  427.     if (lres != ERROR_SUCCESS)
  428.         return FALSE;
  429.  
  430.     lres = RegQueryValueEx(hKey, pszName, NULL, &lType, pvBuf, plSize);
  431.  
  432.     RegCloseKey(hKey);
  433.  
  434.     if (lres != ERROR_SUCCESS)
  435.         return FALSE;
  436.  
  437.     return TRUE;
  438.  
  439. }
  440.  
  441. BOOL SaveWindowState(HWND hWnd, const char *pszName)
  442. {
  443.     RECT rc;
  444.     
  445.     GetWindowRect(hWnd, &rc);
  446.     MapWindowPoints(NULL, mainwindow, (POINT *)&rc, 2);
  447.     return SaveWindowInfo(pszName, &rc, sizeof(rc));
  448. }
  449.  
  450.  
  451. BOOL RestoreWindowState(HWND hWnd, const char *pszName)
  452. {
  453.     RECT rc;
  454.     LONG lSize = sizeof(rc);
  455.  
  456.     if (GetWindowInfo(pszName, &rc, &lSize))
  457.     {
  458.         if (rc.left < 0)
  459.             rc.left = 0;
  460.         if (rc.top < 0)
  461.             rc.top = 0;
  462.         if (rc.right < rc.left + 16)
  463.             rc.right = rc.left + 16;
  464.         if (rc.bottom < rc.top + 16)
  465.             rc.bottom = rc.top + 16;
  466.  
  467.         MoveWindow(hWnd, rc.left, rc.top, rc.right - rc.left, 
  468.                 rc.bottom - rc.top, FALSE);
  469.         return TRUE;
  470.     }
  471.  
  472.     return FALSE;
  473. }
  474.  
  475.